package fr.vvins.core.biz.back;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import fr.vvins.core.common.dto.DtoFactory;
import fr.vvins.core.common.dto.EntityDtoConverter;


public abstract class BackManagerImpl implements BackManager {
	
	private final static Logger log = LoggerFactory.getLogger(BackManagerImpl.class);
	@Autowired
	private DtoFactory dtoFactory;
	@Autowired
	private EntityDtoConverter converter;

/*
	@Override
	@Transactional
	public BeanDto create(BeanDto dto) throws BackException, TechnicalException {
		String entityName = dtoFactory.getEntityName(dto.getDtoName());
		GenericDao dao = daoFactory.getDao(Class.forName((dtoFactory.getEntityClassMapping().get(entityName))));
		Entity entity = dtoFactory.createEntity(dto.getDtoName());
		BeanUtils.copyProperties(dto, entity);
		try {
			entity = (Entity) dao.persist(entity);
			BeanUtils.copyProperties(entity, dto);
		} catch (DataException e) {
			throw new TechnicalException(TechnicalException.DATABASE, "Erreur lors de la creation des donnes " + entityName);
		}
		return dto;
	}

	@Override
	public List<BeanDto> list(String dto) throws BackException, TechnicalException {
		String entityName = dtoFactory.getEntityName(dto);
		GenericDao dao = daoFactory.getDao(entityName);
		List<Dto> dtos = new ArrayList<Dto>();
		try {
			List<Entity> results = dao.getAll();
			for(Entity entity : results){
				Dto aDto = dtoFactory.createDto(dto);
				BeanUtils.copyProperties(entity, aDto);
				dtos.add(aDto);
			}
		} catch (DataException e) {
			throw new TechnicalException(TechnicalException.DATABASE, "Erreur lors de la récupération des donnes " + entityName);
		}
		return dtos;
	}

	@Override
	public Dto update(Dto dto) throws BackException, TechnicalException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Dto delete(Dto dto) throws BackException, TechnicalException {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Dto> search(String dto, String query, Object[] params)
			throws BackException, TechnicalException {
		String entityName = dtoFactory.getEntityName(dto);
		GenericDao dao = daoFactory.getDao(entityName);
		List<BeanDto> dtos = new ArrayList<BeanDto>();
		try {
			List<Entity> results = dao.find(query);
			for(Entity entity : results){
				BeanDto aDto = dtoFactory.createDto(dto);
				BeanUtils.copyProperties(entity, aDto);
				dtos.add(aDto);
			}
		} catch (DataException e) {
			throw new TechnicalException(TechnicalException.DATABASE, "Erreur lors de la récupération des donnes " + entityName);
		}
		return dtos;
	}

	public DtoFactory getDtoFactory() {
		return dtoFactory;
	}

	public void setDtoFactory(DtoFactory dtoFactory) {
		this.dtoFactory = dtoFactory;
	}
	
	public DaoFactory getDaoFactory() {
		return daoFactory;
	}

	public void setDaoFactory(DaoFactory daoFactory) {
		this.daoFactory = daoFactory;
	}

	public EntityDtoConverter getConverter() {
		return converter;
	}

	public void setConverter(EntityDtoConverter converter) {
		this.converter = converter;
	}

*/
}
